import { createSlice } from '@reduxjs/toolkit';
import type { PayloadAction } from '@reduxjs/toolkit';

// 定义state的类型
interface CounterState {
  value: number;
  status: 'idle' | 'loading' | 'failed';
}

// 初始状态
const initialState: CounterState = {
  value: 0,
  status: 'idle',
};

// 创建counter slice
export const counterSlice = createSlice({
  name: 'counter',
  initialState,
  // 同步reducer
  reducers: {
    increment: (state) => {
      state.value += 1;
    },
    decrement: (state) => {
      state.value -= 1;
    },
    incrementByAmount: (state, action: PayloadAction<number>) => {
      state.value += action.payload;
    },
  },
});

// 导出actions
export const { increment, decrement, incrementByAmount } = counterSlice.actions;

// 导出reducer
export default counterSlice.reducer;